import axios from "axios"
import { ElMessageBox } from 'element-plus'

import { useStore } from '@/store/store'

//创建axios实例
const instance = axios.create({
	//默认前缀
	baseURL: process.env.VUE_APP_BASE_URL,
	timeout: 1800000 //请求超时时间
})


//请求拦截器,设置token
instance.interceptors.request.use(request => {

	// 是否需要设置 token
	const isToken = (request.headers || {}).isToken === false

	// let token = useStore().token
	let token = localStorage.getItem('token')

	if (token && !isToken) {
		request.headers['Authorization'] = 'Bearer ' + token
	}
	return request
}, error => {
	return Promise.reject(error)
})

//响应拦截器
instance.interceptors.response.use(response => {
	const data = response.data
	const status = response.status
	if (status === 200 ) {
		return data
	}
}, error => {})

export default instance